
use "~\Meta Harvard LAPS dataverse.dta", clear

keep if includedalton==1

gen proper_baseline=1 if properbaselineformalsector=="Yes"
replace proper_baseline=0 if properbaselineformalsector=="No"

*getting counts of estimates
label define region 1 Latin 2 LDC 3 OECD 4 World
encode region, gen(region_n) label(region)
drop region
rename region_n region

tab dvabbrev if region<3 
summ coeffoninformal if dvabbrev=="Vote Left" & region<3 

* Data Management. Take average of models that are almost the same *
collapse (firstnm) compulsory unpublished ivabbrev country table pensiontype codingofdvbinaryordinalcontinuou dvabbrev definitionofinformalityiv codingofinformalityivbinaryordin region label countries journalpublication journalname journalimpactfactor averageyearofdatacollection proper_baseline leftincumbent leftpreviouslyinitiatedcct sector (mean) n numberofmodelestimatorsk degreesoffreedomnk coeffoninformal seoninformality coeffoninformal2 seoninformality2 coeffonwealth seonwealth coeffonincome seonincome coeffoneducation seoneducation tstatoninformality partialcorrelationoninformalityr tstatonwealth partialcorrelationonwealthr tstatonincome partialcorrelationonincomer tstatoneducation partialcorrelationoneducationr (max) initiationdateofcct initiationdateofhealthcare initiationdateofpensions yearpublished  organizationdensityhummel tradeuniondensityfromilo bouldingpartysystem1mass robertspartysystem1labor epl, by(source modelsetup )
replace org=org/100

gen formal_density=trade/(100-sector)

tab dvabbrev if region<3 
summ coeffoninformal if dvabbrev=="Vote Left" & region<3 

*Precision
gen precision_informal_coeff=1/seoninformality
* Weights 
gen weightFE=1/(seoninformality^2)
gen weightREto=1/(seoninformality^2+0.042)

gen se_informal_partial=((1-partialcorrelationoninformalityr^2)/degreesoffreedomnk)^.5 
*gen precision_informal_partial=1/se_informal_partial

*Calculating Average Effects
	*TURNOUT
	*1. uweighted mean
mean coeffoninformal if dvabbrev=="Turnout" & region<3 

	*2. weighted mean by Ns, p. 46 of Stanley
mean coeffoninformal if dvabbrev=="Turnout" & region<3  [aweight=n]

	*3. FEE, weighted mean by optimal weights, p. 46 of Stanley
mean coeffoninformal if dvabbrev=="Turnout" & region<3  [aweight=weightFE]
metaan coeffoninformal seoninformality if dvabbrev=="Turnout" & region<3 , fe

	*4. REE, weighted mean, p. 46 of Stanley. I use country rather than study as the source of heterogeneity
mean coeffoninformal if dvabbrev=="Turnout" & region<3  [aweight=weightREto]
metaan coeffoninformal seoninformality if dvabbrev=="Turnout" & region<3 , dl label(source)
local REEto=r(eff)
display `REEto'

	*5. intercept is FEE corrected for publication bias, page 60-1 of Stanley, eq 4.1 with variance
gen varoninformality=seoninformality^2
reg coeffoninformal varoninformality if dvabbrev=="Turnout" & region<3 [aweight=weightFE] 

	*LEFT VOTE CONTINUOUS DV
	*1. uweighted mean
mean coeffoninformal if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou=="Continuous"

	*2. weighted mean by Ns, p. 46 of Stanley
mean coeffoninformal if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou=="Continuous" [aweight=n]

	*3. FEE, weighted mean by optimal weights, p. 46 of Stanley
mean coeffoninformal if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou=="Continuous"  [aweight=weightFE]
metaan coeffoninformal seoninformality if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou=="Continuous" , fe

	*4. REE, weighted mean, p. 46 of Stanley.
metaan coeffoninformal seoninformality if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou=="Continuous", dl label(source)
local REEvl=r(eff)
display `REEvl'

		*also getting partial corr 
metaan partialcorrelationoninformalityr se_informal_partial if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou=="Continuous", dl label(source)

	*5. intercept is FEE corrected for publication bias, page 60-1 of Stanley, eq 4.1 with variance
reg coeffoninformal varoninformality if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou=="Continuous" [aweight=weightFE] 

	*LEFT VOTE BINARY DV
	*1. uweighted mean
mean coeffoninformal if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou~="Continuous"

	*2. weighted mean by Ns, p. 46 of Stanley
mean coeffoninformal if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou~="Continuous" [aweight=n]

	*3. FEE, weighted mean by optimal weights, p. 46 of Stanley
mean coeffoninformal if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou~="Continuous"  [aweight=weightFE]
metaan coeffoninformal seoninformality if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou~="Continuous" , fe

	*4. REE, weighted mean, p. 46 of Stanley. 
metaan coeffoninformal seoninformality if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou~="Continuous", dl label(source)
local REEvlbin=r(eff)
display `REEvlbin'

	*5. intercept is FEE corrected for publication bias, page 60-1 of Stanley, eq 4.1 with variance
reg coeffoninformal varoninformality if dvabbrev=="Vote Left" & region<3 & codingofdvbinaryordinalcontinuou~="Continuous" [aweight=weightFE] 

*coefplot meantouLatAm meantofeLatAm meantoreLatAm || meanlvuLatAm meanlvfeLatAm meanlvreLatAm, xlabel("") plotlabels("Unweighted" "Fixed Effects" "Random Effects") bylabels("Turnout" "Left Vote") vertical yline(0)

* Funnel plots *
gen statsig=1 if tstatoninformality>1.9645 |  tstatoninformality<-1.9645  
recode statsig .=0
replace statsig=. if tstatoninformality==.

tab statsig if dvabbrev=="Turnout"
tab statsig if dvabbrev=="Vote Left"

gen labelsig= "{bf:" + label + "}" 

*Unfortunately i don't know of any other way to get the CI width right other than eyeballing it with lwidth
** Latin Plots **
* Turnout *
twoway (scatter precision_informal_coeff coeffoninformal if dvabbrev=="Turnout" & statsig==1 & region<3, msym(o) mcolor(black) mlabel(label) mlabpos(9) mlabsize(vsmall) mlabcolor(black) mlabgap(.1)) ///
(scatter precision_informal_coeff coeffoninformal if dvabbrev=="Turnout" & statsig==0 & region<3, msym(o) mcolor(black) mlabel(label) mlabpos(9) mlabsize(vsmall) mlabcolor(gs10) mlabgap(.1)), ///
graphregion(color(white)) legend(off) xscale(range(-1.25 1.25)) xlabel(-1.25(.25)1.25) xtitle("{bf:Logit Coefficient on Informality}") ytitle("{bf:Precision of Estimates}") ///
xline(`REEto', lwidth(6.1) lc(gs15)) xline(`REEto', lc(black)) plotregion(lstyle(yxline) lcolor(black)) xline(0, lcolor(black) lpattern(dash)) text(21.25 `REEto' "{it:ɛ̄}")  

* LEFT VOTE* CONTINUOUS DV
twoway (scatter precision_informal_coeff coeffoninformal if dvabbrev=="Vote Left" & statsig==1 & region<3 & codingofdvbinaryordinalcontinuou=="Continuous", msym(o) mcolor(black) mlabel(label) mlabpos(9) mlabsize(vsmall) mlabcolor(black)) ///
(scatter precision_informal_coeff coeffoninformal if dvabbrev=="Vote Left" & statsig==0 & region<3 & codingofdvbinaryordinalcontinuou=="Continuous", msym(o) mcolor(black) mlabel(label) mlabpos(9) mlabsize(vsmall) mlabcolor(gs10)), ///
graphregion(color(white)) legend(off) xscale(range(-2.25 2.25)) xlabel(-2(.5)2) xtitle("{bf:OLS Coefficient on Informality}") ytitle("{bf:Precision of Estimates}") ///
xline(`REEvl', lwidth(5.8) lc(gs15)) xline(`REEvl', lc(black)) plotregion(lstyle(yxline) lcolor(black)) xline(0, lcolor(black) lpattern(dash)) text(10.5 `REEvl' "{it:ɛ̄}")

* LEFT VOTE* Binary DV
twoway (scatter precision_informal_coeff coeffoninformal if dvabbrev=="Vote Left" & statsig==1 & region<3 & codingofdvbinaryordinalcontinuou~="Continuous", msym(o) mcolor(black) mlabel(label) mlabpos(9) mlabsize(vsmall) mlabcolor(black)) ///
(scatter precision_informal_coeff coeffoninformal if dvabbrev=="Vote Left" & statsig==0 & region<3 & codingofdvbinaryordinalcontinuou~="Continuous", msym(o) mcolor(black) mlabel(label) mlabpos(9) mlabsize(vsmall) mlabcolor(gs10)), ///
graphregion(color(white)) legend(off) xscale(range(-1 1)) xlabel(-1(.25)1) xtitle("{bf:Logit Coefficient on Informality}") ytitle("{bf:Precision of Estimates}") ///
xline(`REEvlbin', lwidth(11.5) lc(gs15)) xline(`REEvlbin', lc(black)) plotregion(lstyle(yxline) lcolor(black)) xline(0, lcolor(black) lpattern(dash)) text(10.5 `REEvlbin' "{it:ɛ̄}")

*** Meta Regression Analysis ***
encode dvabbrev, gen(DV)
encode ivabbrev, gen(IV)

gen inf_ben = 0
replace inf_ben = 1 if IV==1
replace inf_ben = 1 if IV==2

encode codingofinformalityivbinaryordin, gen(infcode)
gen binary = 0
replace binary = 1 if infcode==18

encode journalpublication, gen(journal)
recode journal 1=0 2=1
recode journalimpactfactor .=0

gen yeardiffcct = initiationdateofcct - averageyearofdatacollection
gen cct = .
replace cct = 1 if yeardiffcct > -1 & yeardiffcct !=.
replace cct = 0 if yeardiffcct < 0 & yeardiffcct !=.

gen yeardiffhealth = initiationdateofhealthcare - averageyearofdatacollection
gen health_u = .
replace health_u = 0 if yeardiffhealth < 0 & yeardiffhealth!=.
replace health_u = 1 if yeardiffhealth > -1 & yeardiffhealth!=.

encode pensiontype, gen(pentype)
gen pension_u = 0
replace pension_u = 1 if pentype==2
gen pension_m = 0
replace pension_m = 1 if pentype==1

gen yeardiffpen_u = initiationdateofpensions - averageyearofdatacollection if pension_u==1
gen universal_pen = .
replace universal_pen = 0 if yeardiffpen_u < 0 & pension_u==1 & yeardiffpen_u !=.
replace universal_pen = 1 if yeardiffpen_u > -1 & pension_u==1 & yeardiffpen_u !=.

gen yeardiffpen_m = initiationdateofpensions - averageyearofdatacollection if pension_m==1
gen meanstest_pen = .
replace meanstest_pen = 0 if yeardiffpen_m < 0 & pension_m==1
replace meanstest_pen = 1 if yeardiffpen_m > -1 & pension_m==1

gen services = cct + health_u + universal_pen if region==1
recode services .=0 if region==1
recode services 0=. if source==13

gen vote_int = 0
recode vote_int 0=1 if source==6 | source==18

gen income = 0
replace income = 1 if coeffonincome!=.
replace income = 1 if coeffonwealth!=. & income==0

gen educ = 0
replace educ = 1 if coeffoneduc!=.

recode compulsory 0=1 1=1

label define yn 0 "No" 1 "Yes"

label values inf_ben yn
label values journal yn

label variable averageyear "Year"
label variable compulsory "Compulsory voting"
label variable income "Controls for income"
label variable educ "Controls for education"
label variable inf_ben "Benefits definition"
label variable unpublished "Unpublished"
label variable precision_informal_coeff "Precision of the estimate"


** REGRESSIONS ** 
	*TURNOUT
	*Table 2
metareg coeffoninformal org boulding compulsory averageyearofdatacollection income if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
margin, at(compulsory=0)
margin, at(compulsory=3)
margin, at(org=.11)
margin, at(org=.33)

metareg coeffoninformal averageyearof income inf_ben unpublished if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
margin, at(averageyearofdatacollection=1995)
margin, at(averageyearofdatacollection=2018)

	*Appendix: Part C
metareg coeffoninformal org income if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
metareg coeffoninformal boulding income if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
metareg coeffoninformal compulsory income if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
metareg coeffoninformal averageyearofdatacollection income if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
metareg coeffoninformal income if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
metareg coeffoninformal inf_ben if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)
metareg coeffoninformal unpublished if dvabbrev=="Turnout" & region<3 , wsse(seoninformality)

	*VOTE CHOICE
*Vote Left (Binary)
	*Part B of Online App 
metareg coeffoninformal org epl leftpreviouslyinitiatedcct averageyearofdatacollection income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou~="Continuous"  , wsse(seoninformality)

*Vote Left (Continuous)
	*Part B of Online App 
metareg coeffoninformal org epl leftpreviouslyinitiatedcct averageyearofdatacollection income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou=="Continuous"  , wsse(seoninformality)

*Vote Left (Binary)
	*Appendix: Part C
metareg coeffoninformal org income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou~="Continuous"  , wsse(seoninformality)
metareg coeffoninformal epl income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou~="Continuous"  , wsse(seoninformality)
metareg coeffoninformal leftpreviouslyinitiatedcct income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou~="Continuous"  , wsse(seoninformality)
metareg coeffoninformal averageyearofdatacollection income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou~="Continuous"  , wsse(seoninformality)
metareg coeffoninformal income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou~="Continuous"  , wsse(seoninformality)

*Vote Left (Continuous)
	*Appendix: Part C
metareg coeffoninformal org income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou=="Continuous"  , wsse(seoninformality)
metareg coeffoninformal epl income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou=="Continuous"  , wsse(seoninformality)
metareg coeffoninformal leftpreviouslyinitiatedcct income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou=="Continuous"  , wsse(seoninformality)
metareg coeffoninformal averageyearofdatacollection income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou=="Continuous"  , wsse(seoninformality)
metareg coeffoninformal income inf_ben unpub if dvabbrev=="Vote Left" & region==1 & codingofdvbinaryordinalcontinuou=="Continuous"  , wsse(seoninformality)

